package com.charger.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.charger.chargingStation.domain.Chargingstation;
import com.charger.chargingStation.service.IChargingstationService;
import com.charger.domain.Dispose;
import com.charger.service.IDisposeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.domain.Risks;
import com.charger.service.IRisksService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 隐患故障中隐患的具体信息Controller
 *
 * @author xuan
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/risksFailure/risksController")
public class RisksController extends BaseController {
    @Autowired
    private IRisksService risksService;
    @Autowired
    private IDisposeService disposeService;
    @Autowired
    private IChargingstationService chargingstationService;

    /**
     * 查询隐患故障中隐患的具体信息列表
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksController:list')")
    @GetMapping("/list")
    public TableDataInfo list(Risks risks) {
        startPage();
        List<Risks> list = risksService.selectRisksList(risks);
        list.forEach(fai -> {
            // 修改处理表
            if (fai.getDisposalId() != null)
                fai.setDispose(disposeService.selectDisposeById(fai.getDisposalId()));
            // 修改充电站
            if (fai.getChargerId() != null)
                fai.setChargingstation(chargingstationService.selectChargingstationByChargingstationId(fai.getChargerId()));
        });
        return getDataTable(list);
    }

    /**
     * 导出隐患故障中隐患的具体信息列表
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksController:export')")
    @Log(title = "隐患故障中隐患的具体信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Risks risks) {
        List<Risks> list = risksService.selectRisksList(risks);
        ExcelUtil<Risks> util = new ExcelUtil<Risks>(Risks.class);
        util.exportExcel(response, list, "隐患故障中隐患的具体信息数据");
    }

    /**
     * 获取隐患故障中隐患的具体信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksController:query')")
    @GetMapping(value = "/{risksId}")
    public AjaxResult getInfo(@PathVariable("risksId") Long risksId) {
        Risks risks = risksService.selectRisksByRisksId(risksId);
        // 修改处理表
        if (risks.getDisposalId() != null)
            risks.setDispose(disposeService.selectDisposeById(risks.getDisposalId()));
        // 修改充电站
        if (risks.getChargerId() != null)
            risks.setChargingstation(chargingstationService.selectChargingstationByChargingstationId(risks.getChargerId()));
        return success(risks);
    }

    /**
     * 新增隐患故障中隐患的具体信息
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksController:add')")
    @Log(title = "隐患故障中隐患的具体信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Risks risks) {
        risks.setState("未派单");
        return toAjax(risksService.insertRisks(risks));
    }

    /**
     * 修改隐患故障中隐患的具体信息
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksController:edit')")
    @Log(title = "隐患故障中隐患的具体信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Risks risks) {
        if (risks.getDispose() == null) {
            // 派发
            risks.setDisposalId(risks.getDisposalId());
            risks.setState("未处理");
            // 派发添加
            Dispose parse = disposeService.selectDisposeById(risks.getDisposalId());
            Dispose dispose = new Dispose();
            dispose.setProcessorsName(parse.getProcessorsName());
            disposeService.insertDispose(dispose);
            risks.setDisposalId(disposeService.selectDisposeById2());
        } else {
            // 修改处理表
            disposeService.updateDispose(risks.getDispose());
        }
        // 修改充电站
        Chargingstation chargingstation = risks.getChargingstation();
        Long id = chargingstation.getChargingstationId();
        risks.setChargerId(id);
        return toAjax(risksService.updateRisks(risks));
    }

    /**
     * 删除隐患故障中隐患的具体信息
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:risksController:remove')")
    @Log(title = "隐患故障中隐患的具体信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{risksIds}")
    public AjaxResult remove(@PathVariable Long[] risksIds) {
        return toAjax(risksService.deleteRisksByRisksIds(risksIds));
    }
}
